package com.kexuexiong.demo.bear.controller.file;

import cn.hutool.http.ContentType;
import com.kexuexiong.demo.bear.common.dto.Result;
import com.kexuexiong.demo.bear.common.utils.BearMultipartFile;
import com.kexuexiong.demo.bear.common.utils.MinioUtil;
import com.kexuexiong.demo.bear.controller.BaseController;
import com.kexuexiong.demo.common.feignProvider.baseService.file.FileFeignClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * @author xy
 */
@RestController
@RequestMapping("api/file/")
@Slf4j
@Api(tags = {"文件"})
public class FileIndexController extends BaseController {

    @Autowired
    private MinioUtil minioUtil;

    @Autowired
    private FileFeignClient fileFeignClient;

    /**
     * 上传
     *
     * @param file
     * @return
     */
    @RequestMapping(value = "/uploadFile", method = {RequestMethod.POST})
    @ApiOperation(value = "上传文件", notes = "上传文件")
    public Result<String> uploadFile(@Parameter(name = "file", required = true) @RequestParam("file") MultipartFile file) {
        log.info("uploadFile,fileName=>{}", file.getName());
        return Result.success(minioUtil.upload(file, file.getOriginalFilename()));
    }

    /**
     * 上传
     *
     * @param file
     * @return
     */
    @RequestMapping(value = "/uploadFile/baseService", method = {RequestMethod.POST})
    @ApiOperation(value = "上传文件到baseService", notes = "上传文件到baseService")
    public Result<String> uploadFile2BaseService(@Parameter(name = "file", required = true) @RequestParam("file") MultipartFile file) throws IOException {
        log.info("uploadFile2BaseService,fileName=>{}", file.getName());
        fileFeignClient.uploadFile(file);
        return Result.success();
    }

    /**
     * 上传
     *
     * @param file
     * @return
     */
    @RequestMapping(value = "/template/uploadFile/baseService", method = {RequestMethod.POST})
    @ApiOperation(value = "上传文件到baseService", notes = "上传文件到baseService")
    public Result<String> templateUploadFile2BaseService(@Parameter(name = "file", required = true) @RequestParam("file") String file) throws Exception {
        log.info("templateUploadFile2BaseService,fileName=>{}", file);
        BearMultipartFile multipartFile = new BearMultipartFile("file", null, ContentType.TEXT_PLAIN.getValue(), file);
        fileFeignClient.uploadFile(multipartFile);
        return Result.success();
    }

    /**
     * 获取预览url
     *
     * @param objectName
     * @return
     */
    @RequestMapping(value = "/previewFile", method = {RequestMethod.GET})
    @ApiOperation(value = "获取预览url", notes = "获取预览url")
    public Result<String> previewFile(@Parameter(name = "objectName", required = true) @RequestParam("objectName") String objectName) {
        log.info("previewFile,objectName=>{}", objectName);
        return Result.success(minioUtil.getUrl(objectName, 5, TimeUnit.MINUTES));
    }
}
